package in.muton.edushare.pages.coursepages;

import in.muton.edushare.auth.roles.CourseRoleCheck;
import in.muton.edushare.panels.assignment.AssignmentListPanel;
import in.muton.edushare.wicket.models.CayenneModel;

import java.util.HashMap;

import org.apache.wicket.PageParameters;
import org.apache.wicket.markup.html.WebMarkupContainer;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.link.Link;
import org.apache.wicket.model.PropertyModel;

import com.edushare.common.constants.ApplicationConstants;
import com.edushare.common.dao.AssignmentsDAO;
import com.edushare.common.vo.Assignments;
import com.edushare.common.vo.CourseInstances;

public class CourseAssignmentList extends CourseBase {

	private HashMap<Integer, Integer> statMap;
	private boolean countDraft;

	public CourseAssignmentList(final PageParameters params) {
		super(params);
		CourseInstances ci = getCourseInstance().getObject();
		Link<Void> createAssignment = new Link<Void>("createAssignment"){
			private static final long serialVersionUID = -6637004656509607703L;

			@Override
			public void onClick() {
				setResponsePage(CourseAssignmentCreation.class, params);
			}
			
		};
		
		if(!CourseRoleCheck.isAdmin(ci, getLoggedInUser())&&!CourseRoleCheck.isTeacher(ci, getLoggedInUser())){
			createAssignment.setVisible(false);
		}
		
		add(createAssignment);
		
		this.statMap = new AssignmentsDAO(getSessionORMInterface()).getStatusStat(ci);
		this.countDraft = true;
		add(new Label("openCount", new PropertyModel<String>(this, "openCount")));//wicket:id="openCount" 
		add(new Label("closedCount", new PropertyModel<String>(this, "closedCount")));
		WebMarkupContainer draftContainer = new WebMarkupContainer("showDraftStat");
		add(draftContainer);
		draftContainer.add(new Label("draftCount", new PropertyModel<String>(this, "draftCount")));
		if(!CourseRoleCheck.isAdmin(ci, getLoggedInUser()) && !CourseRoleCheck.isTeacher(ci, getLoggedInUser())){
			draftContainer.setVisible(false);
			countDraft = false;
		}
		add(new Label("allCount", new PropertyModel<String>(this, "allCount")));
		
		
		add(new AssignmentListPanel("assnList", new CayenneModel<CourseInstances>(ci)));
	}

	@Override
	protected String getActivePage(PageParameters params) {
		return "viewAssn";
	}

	@Override
	public String getPageTitle() {
		return getLoggedInUser().getCompleteUserName() + " | "
		+ getCourse().getObject().getCourseName()
		+ " | Assignments | "
		+ ApplicationConstants.APPLICATION_NAME;		
	}
	
	public int getAllCount(){
		int n = getClosedCount() + getOpenCount();
		if(this.countDraft)
			n = n+getDraftCount();
		return n;
	}
	
	public int getClosedCount(){
		if(this.statMap.get(Assignments.ASSIGNMENT_STATUS_CLOSED)!=null){
			return this.statMap.get(Assignments.ASSIGNMENT_STATUS_CLOSED);
		}else{
			return 0;
		}
	}
	
	public int getOpenCount(){
		if(this.statMap.get(Assignments.ASSIGNMENT_STATUS_OPEN)!=null){
			return this.statMap.get(Assignments.ASSIGNMENT_STATUS_OPEN);
		}else{
			return 0;
		}
	}
	
	public int getDraftCount(){
		if(this.statMap.get(Assignments.ASSIGNMENT_STATUS_DRAFT)!=null){
			return this.statMap.get(Assignments.ASSIGNMENT_STATUS_DRAFT);
		}else{
			return 0;
		}
	}

}
